var tips;
var app = getApp()
Page({
  data:{
    modalHidden: true,
    current: 0,
    
    price: [{
      money: "2.00",
    }]
  },
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh')
  },

  
  onLoad: function (options) {
    this.initSystemInfo();
    this.setData({
      currentTab: parseInt(options.tips),
      //isStatus: options.otype
    });

    // if (this.data.currentTab == 4) {
    //   this.loadReturnOrderList();
    // } else {
    //   this.loadOrderList();
    // }
    var tips = options.tips;
    var uid = app.whereId.uid;
    var shopid = app.whereId.shopid;
    var that = this;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/order/index',
      data: {
        tag: tips,
        uid: uid,
        shopid:shopid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        console.log(res); console.log("--------");
        that.setData({
          cakes: res.data.cakes,
        });
      },
    })
    this.setData({
      tips: tips,
    });
  },

  zhis: function (e) {
    var tid = e.target.dataset.id;  //点击的id 判断是待付还是待发等等
    //console.log(tid);//id值
  },
  swichNav: function (e) {
    var tag = e.currentTarget.dataset.current;
    var uid = app.whereId.uid;
    var shopid = app.whereId.shopid;
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({
        currentTab: parseInt(current),
        isStatus: e.target.dataset.otype,
      });

      //没有数据就进行加载
      // switch (that.data.currentTab) {
      //   case 0:
      //     !that.data.orderList0.length && that.loadOrderList();
      //     break;
      //   case 1:
      //     !that.data.orderList1.length && that.loadOrderList();
      //     break;
      //   case 2:
      //     !that.data.orderList2.length && that.loadOrderList();
      //     break;
      //   case 3:
      //     !that.data.orderList3.length && that.loadOrderList();
      //     break;
      //   case 4:
      //     that.data.orderList4.length = 0;
      //     that.loadReturnOrderList();
      //     break;
      // }
    };
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/order/index',
      data: {
        tag: tag,
        uid: uid,
        shopid:shopid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        console.log(res); console.log("---1");
        that.setData({
          cakes: res.data.cakes,
        });
      },
    })
  },
  getOrderStatus: function () {
    return this.data.currentTab == 0 ? 1 : this.data.currentTab == 2 ? 2 : this.data.currentTab == 3 ? 3 : 0;
  },

  initSystemInfo: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  }, 

  modalTap: function (e) {
    this.setData({
      modalHidden: false
    })
  },
  modalChange: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  modalTap2: function (e) {
    this.setData({
      modalHidden2: false
    })
  },
  modalChange2: function (e) {
    this.setData({
      modalHidden2: true
    })
  },

  //详情页跳转
  lookdetail:function(e){
    var lookid=e.currentTarget.dataset;
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url:"/pages/yiguo/detail/detail?id="+lookid.id
    })
  },


  switchSlider: function (e) {
    var that = this;
    if (that.data.current === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({
        currentTab: parseInt(current),
        isStatus: e.target.dataset.otype,
      });

      //没有数据就进行加载
      switch (that.data.currentTab) {
        case 0:
          !that.data.orderList0.length && that.loadOrderList();
          break;
        case 1:
          !that.data.orderList1.length && that.loadOrderList();
          break;
        case 2:
          !that.data.orderList2.length && that.loadOrderList();
          break;
        case 3:
          !that.data.orderList3.length && that.loadOrderList();
          break;
        case 4:
          that.data.orderList4.length = 0;
          that.loadReturnOrderList();
          break;
      }
    };
  },

  
  changeSlider:function(e){
    this.setData({
      current: e.detail.current
    })
  },

  comment: function (e) {
    wx.navigateTo({
      url: "/pages/yiguo/comment/comment"
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

//取消订单
  df_quxiao: function (e) {
    var that = this;
    var ide = e.currentTarget.dataset.id;
    var currentTabe = e.currentTarget.dataset.name;
    var uid = app.whereId.uid;

    wx.showModal({
      title: '提示',
      content: '确认取消该订单？',
      success: function (res) {
        if (res.confirm == true){
            wx.request({
            url: 'https://www.cakeyouxuan.com/small/order/quxiao',
            data: {
              id: ide,
              uid: uid,
              tag: currentTabe,
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"  //post
            },
            success: function (res) {
                wx.showToast({
                  title: '订单已取消',
                  icon: 'success',
                  duration: 2000,
                  success: function () {
                    that.setData({
                      currentTab: currentTabe,
                      cakes: res.data.cakes,
                    })
                  },
                })
            },
          })
        }else{
        }
      },
    }) 
  },

  //确认收货
  shouhuo:function(e){
    var that = this;
    var ide = e.currentTarget.dataset.id;
    var currentTabes = e.currentTarget.dataset.name;
    var uid = app.whereId.uid;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/order/shouhuo',
      data: {
        id: ide,
        uid: uid,
        tag: currentTabes,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: '收货成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            that.setData({
              currentTab: currentTabes,
              cakes: res.data.cakes,
            })
          }
        })
      },
    })
  }, 

  //评价
  pingjia:function(e){  
    var ide = e.currentTarget.dataset.id;
    var currentTabe = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: "/pages/yiguo/comment/comment?ide=" + ide,
    })
  },

//进入订单内页
  //全部info
  all_info:function(e){
    this.data.navigateToNextDelta = true
    var id = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: "/pages/yiguo/orders/orders?ide=" + id,
    })
  },
  //待付info
  daifu_info: function (e) {
    this.data.navigateToNextDelta = true
    var id = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: "/pages/yiguo/orders/orders?ide=" + id,
    })
  },
  //待发info
  daifa_info: function (e) {
    this.data.navigateToNextDelta = true
    var id = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: "/pages/yiguo/orders/orders?ide=" + id,
    })
  },
  //待收info
  daishou_info: function (e) {
    this.data.navigateToNextDelta = true
    var id = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: "/pages/yiguo/orders/orders?ide=" + id,
    })
  },
  //评价info
  pingjia_info: function (e) {
    this.data.navigateToNextDelta = true
    var id = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: "/pages/yiguo/orders/orders?ide=" + id,
    })
  },

  //提醒发货
  tx_fahuo:function(){
    wx.showToast({
      title: '已提醒商家',
      icon: 'success',
      duration: 2000,
      success: function () {
      }
    })
  },

  ///确认付款
  payfor:function(e){
    var id= e.currentTarget.dataset.id;  //订单id
    var openid = app.globalData.openid; //openid
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/payinskpz/index',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        id:id,
        openid: openid,
      },
      success: function (res) {
        console.log(res);
        if (res.data.state == 1) {
          // --------- 订单生成成功，发起支付请求 ------------------
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,   //字符串随机数
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            'success': function (res) {
              //requestPayment:ok==>调用支付成功
              if (res.errMsg == "requestPayment:ok") {
                wx.request({
                  url: 'https://www.cakeyouxuan.com/small/payin/notifys',
                  data: {
                    id: id,
                  },
                  method: "POST",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"  //post
                  },
                  success: function (res) {
                    wx.navigateTo({
                      url: "/pages/yiguo/success/success?price=" + res.data.price + "&id=" + res.data.ids,
                    })
                  },
                })
              }
            },
            'fail': function (res) {
                console.log(res);
            },
            'complete': function (res) {

            }
          })
        } else if (res.data.state == 0) {
          wx.showToast({
            title: res.data.Msg,
            icon: 'fail',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: '系统繁忙，请稍后重试~',
            icon: 'fail',
            duration: 1000
          })
        }
      }
    })
  },
})
